package OSMS.action.goodsfunc;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import OSMS.dao.GoodsDao;
import OSMS.entity.Car;
import OSMS.entity.Customers;
/*
 * 购物车展示页面
 * 	业务逻辑如下：
 * 1、当用户在商城主页面时点击我的购物车，此时不能通过inCarServelet.java来进入showCar.jsp
 * 2、因为此时不能进行任何操作，只能去查看购物车
 * 3、所以写了本文件，获取用户编号cid后直接调用dao里的getCarById方法查找购物车
 * 4、然后重定向到购物车页面进行展示购物车信息。
 */
public class showCarServlet extends HttpServlet{

	private GoodsDao dao = new GoodsDao();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//此处获取的cid就是从首页面中获取的全局变量：用户编码

		HttpSession session = request.getSession();	
		//取session中的cid
		Object obj_c = session.getAttribute("cus");
		Customers cus = (Customers)obj_c;
		int cid = cus.getCid();
		//此处测试：直接赋值
		System.out.println("cid是"+Integer.toString(cid));
		try {
			//调用dao里的getCarById方法查找购物车
			List<Car> carList = dao.getCarById(cid);
			//存放在session域中方便后续订单页面使用
			session.setAttribute("carList",carList);
			//request.getRequestDispatcher("showCar.jsp").forward(request, response);
			//重定向到showCar.jsp购物车页面
			response.sendRedirect(request.getServletContext().getContextPath() + "/jsp/goods/showCar.jsp");
		} catch(Exception ex) {
			ex.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}



}
